package Middle.ZConvert;

public class ZConvert {
    public static void main(String[] args) {
        String  str = "PAYPALISHIRING";
        System.out.println(convert(str, 3));
    }

    public static String convert(String input, int rows) {
        if (rows <= 1) {
            return input;
        }
        StringBuilder[] resultRows = new StringBuilder[rows];
        for (int i = 0; i < rows; i++) {
            resultRows[i] = new StringBuilder();
        }

        //1向上，-1向下
        int direction = 1;
        int currentRow = 0;

        for (char c : input.toCharArray()) {
            resultRows[currentRow].append(c);
            currentRow += direction;
            if (currentRow >= rows - 1 || currentRow <= 0) {
                direction *= -1;
            }
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder row : resultRows) {
            result.append(row);
        }
        return result.toString();
    }
}
